<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller {
    public function index(){
    $userid=1;//session('userid');
    $cartinfo=D('Shopcarts')->where('userid='.$userid)->select();
    if(!$userid){
           $this->redirect('Login/login');
      }
   
    $this->assign('cartinfo',$cartinfo); 
    $this->display();
    }


    public function add(){
      if(!session('userid')){
          return ajaxreturn(0,'');
      }
      $goodsid=$_POST['goodsid'];
      $price=$_POST['price'];
      $num=$_POST['num'];
      $thumb=$_POST['thumb'];
      $name=$_POST['name'];
      $userid=session('userid');
      $createtime=time();
      $r=D('Cart')->where('userid='.$userid.' and goodsid='.$goodsid)->find();
      if(!$r){
          $data=array(
                'goodsid'=>$goodsid,
                'price'=>$price,
                'number'=>$num,
                'userid'=>$userid,
                'thumb'=>$thumb,
                 'name'=>$name,
                'createtime'=>$createtime
        );
        $res=D('Cart')->add($data);
      }else{
            $num+=$r['number'];
            $id=$r['id'];
            $data=array(
              'id'=>$id,
              'number'=>$num,
              'createtime'=>$createtime
        );
        $res=D('Cart')->save($data);
      }
      
      $url=array(
        'jump_url'=>'/index.php?c=Cart&a=index'
        );

      if($res){
         return ajaxreturn(1,'成功加入购物车', $url);
      }else{
         return ajaxreturn(0,'加入购物车失败');
      }
      
    }


    public function del(){
      $goodsid=$_POST['goodsid'];
      $userid=session('userid');
      $res=D('Cart')->where('userid='.$userid.' and goodsid='.$goodsid)->delete();
      if($res){
         return ajaxreturn(1);
      }else{
         return ajaxreturn(0);
      }
    }
 public function delall(){
     
      $userid=session('userid');
      $res=D('Cart')->where('userid='.$userid)->delete();
      if($res){
         return ajaxreturn(1);
      }else{
         return ajaxreturn(0);
      }
    }
    public function info(){
        $userid=session('userid');
        $res=D('Cart')->where('userid='.$userid)->select();
        $totalprice=0;
        $totalnumber=0;
        foreach ($res as $key => $value) {
          $totalprice+=$value['price']*$value['number'];
          $totalnumber++;
        }
        $arr=array(
          'totalprice'=>$totalprice,
          'totalnumber'=>$totalnumber,
          );
      if($res){
         return ajaxreturn(1,'',$arr);
      }else{
         return ajaxreturn(0,'');
      }
    }

    public function updata(){
      $goods_id=$_POST['goodsid'];
      $num=intval($_POST['num']);
      $user_id=session('userid');
       $data=array(
          'number'=>$num
          
        );
      $res=D('Cart')->where('user_id='.$userid.' and goods_id='.$goodsid)->save($data);
      if($res){
         return ajaxreturn(1);
      }else{
         return ajaxreturn(0);
      }
      
    }
}